/**
 * 账单路由
 * 定义账单相关的API路由
 * 严格按照Java项目接口定义迁移
 */
const express = require('express')
const billController = require('../controllers/billController')
const authMiddleware = require('../middlewares/auth')

const router = express.Router()

// 所有账单路由都需要认证
router.use(authMiddleware)

/**
 * @swagger
 * /bills:
 *   get:
 *     summary: 获取账单列表
 *     tags: [账单]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         required: false
 *         schema:
 *           type: integer
 *         description: 页码，默认为1
 *       - in: query
 *         name: size
 *         required: false
 *         schema:
 *           type: integer
 *         description: 每页数量，默认为10
 *     responses:
 *       200:
 *         description: 查询成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
router.get('/bills', billController.getBillList)

/**
 * @swagger
 * /export:
 *   get:
 *     summary: 导出账单数据
 *     tags: [账单]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: startDate
 *         required: false
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期，格式YYYY-MM-DD
 *       - in: query
 *         name: endDate
 *         required: false
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期，格式YYYY-MM-DD
 *     responses:
 *       200:
 *         description: 导出成功
 *         content:
 *           application/vnd.openxmlformats-officedocument.spreadsheetml.sheet:
 *             schema:
 *               type: string
 *               format: binary
 *       400:
 *         description: 参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
router.get('/export', billController.exportBills)

/**
 * @swagger
 * /add:
 *   post:
 *     summary: 添加账单
 *     tags: [账单]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               categoryId:
 *                 type: integer
 *                 description: 分类ID
 *                 example: 1
 *               amount:
 *                 type: number
 *                 description: 金额
 *                 example: 100.50
 *               billDate:
 *                 type: string
 *                 description: 账单日期
 *                 example: '2023-10-15'
 *               type:
 *                 type: string
 *                 description: 收支类型
 *                 enum: [1, 0, income, expense]
 *                 example: '1'
 *               remark:
 *                 type: string
 *                 description: 备注
 *                 example: '午餐'
 *     responses:
 *       200:
 *         description: 添加成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       400:
 *         description: 参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权，需要先登录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
router.post('/add', billController.addBill)

/**
 * @swagger
 * /billByDate:
 *   get:
 *     summary: 按日期查询账单
 *     tags: [账单]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: year
 *         required: true
 *         schema:
 *           type: integer
 *         description: 年份
 *       - in: query
 *         name: month
 *         required: true
 *         schema:
 *           type: integer
 *         description: 月份(1-12)
 *     responses:
 *       200:
 *         description: 查询成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       400:
 *         description: 参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
router.get('/billByDate', billController.getBillsByDate)

/**
 * @swagger
 * /recently:
 *   post:
 *     summary: 获取最近账单
 *     tags: [账单]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权，需要先登录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 获取最近账单
router.post('/recently', billController.getRecentBills)
// 兼容Java项目接口路径
router.post('/recentlyBill', billController.getRecentBills)

/**
 * @swagger
 * /statistics:
 *   post:
 *     summary: 获取近6个月收支数据（柱状图）
 *     tags: [统计]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权，需要先登录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 获取近6个月收支数据（柱状图）
router.post('/statistics', billController.getMonthlyStatistics)

/**
 * @swagger
 * /updateBill:
 *   post:
 *     summary: 修改账单
 *     tags: [账单]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: integer
 *                 description: 账单ID
 *                 example: 1
 *               categoryId:
 *                 type: integer
 *                 description: 分类ID
 *                 example: 1
 *               amount:
 *                 type: number
 *                 description: 金额
 *                 example: 100.50
 *               type:
 *                 type: string
 *                 description: 收支类型
 *                 enum: [1, 0, income, expense]
 *                 example: '1'
 *               remark:
 *                 type: string
 *                 description: 备注
 *                 example: '午餐'
 *               billDate:
 *                 type: string
 *                 description: 账单日期
 *                 example: '2023-10-15'
 *     responses:
 *       200:
 *         description: 修改成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       400:
 *         description: 参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权，需要先登录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       404:
 *         description: 账单不存在或不属于该用户
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 修改账单
router.post('/updateBill', billController.updateBill)

/**
 * @swagger
 * /deleteBill:
 *   post:
 *     summary: 删除账单
 *     tags: [账单]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: integer
 *                 description: 账单ID
 *                 example: 1
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       400:
 *         description: 参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权，需要先登录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       404:
 *         description: 账单不存在或不属于该用户
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 删除账单
router.post('/deleteBill', billController.deleteBill)

/**
 * @swagger
 * /stats:
 *   get:
 *     summary: 按月份统计分类支出（饼图）
 *     tags: [统计]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: year
 *         required: true
 *         description: 年份
 *         schema:
 *           type: integer
 *           example: 2023
 *       - in: query
 *         name: month
 *         required: true
 *         description: 月份
 *         schema:
 *           type: integer
 *           example: 10
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       400:
 *         description: 参数错误，如年份或月份格式不正确
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权，需要先登录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 按月份统计分类支出（饼图）
router.get('/stats', billController.stats)

/**
 * @swagger
 * /trend:
 *   get:
 *     summary: 获取年度账单趋势（折线图）
 *     tags: [统计]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: year
 *         required: true
 *         description: 年份
 *         schema:
 *           type: integer
 *           example: 2023
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       400:
 *         description: 参数错误，如年份格式不正确
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权，需要先登录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 获取年度账单趋势（折线图）
router.get('/trend', billController.getYearlyTrend)

module.exports = router
